package com.kindness.frate.widget;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.util.SparseArray;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.kindnesslibrary.util.ScreenUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/27 0027.
 */
public class SVViewPager extends ViewPager {

    PullZoomVPScroll pullZoomVPScroll;
    private GestureDetector mGestureDetector; // 手势
    private int mScreentHeight = 0; // 屏幕高度
    private boolean once;
    Map<Integer, Integer> values = new HashMap<>();

    public SVViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGestureDetector = new GestureDetector(getContext(), new YScrollDetector());
        mScreentHeight = ScreenUtil.getScreenHeight(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (getChildCount() == 0) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            return;
        }
        int height = 0;
        int index = 0;
        if (getChildCount() <= 2) {
            if (getCurrentItem() != 0) {
                index = getChildCount() - 1;
            }
        } else if (getChildCount() >= 3) {
            index = (getCurrentItem() + 1) % getChildCount();
        }
        if (getCurrentItem() <= 2) {
            index = getCurrentItem();
        }
        View child = getChildAt(index);
        if (child != null) {
            child.measure(widthMeasureSpec,
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            height = child.getMeasuredHeight();
        }
        if (!values.containsKey(getCurrentItem()) && height != 0) {
            values.put(getCurrentItem(), height);
        }
        // 下面遍历所有child的高度
//        if(height == 0) {
//            for (int i = 0; i < getChildCount(); i++) {
//                View child = getChildAt(i);
//                child.measure(widthMeasureSpec,
//                        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
//                int h = child.getMeasuredHeight();
//                // 采用最大的view的高度
//                if (h > height) {
//                    height = h;
//                }
//            }
//            if (!values.containsKey(getCurrentItem()) && height != 0) {
//                values.put(getCurrentItem(), height);
//            }
//        }
        if (values.containsKey(getCurrentItem())) {
            int hei = values.get(getCurrentItem());
            if (hei != height) {
                height = hei;
            }
        }
        RecyclerView recyclerView = null;
        if (child instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) child;
            if(viewGroup.getChildCount() > 0){
                viewGroup = (ViewGroup) viewGroup.getChildAt(0);
                if(viewGroup.getChildCount() > 0) {
                    if (viewGroup.getChildAt(0) instanceof RecyclerView) {
                        recyclerView = (RecyclerView) viewGroup.getChildAt(0);
                    }
                }
            }
        }
        if (height >= 0 && height < mScreentHeight || recyclerView != null) {
            height = mScreentHeight;
        }
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
                MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                once = false;
            }
            break;
            case MotionEvent.ACTION_MOVE: {
                boolean result = mGestureDetector.onTouchEvent(ev);
                if (result && once) {
                    getParent().requestDisallowInterceptTouchEvent(false);
                } else {
                    once = true;
                }
            }
            break;
            case MotionEvent.ACTION_UP: {
                getParent().requestDisallowInterceptTouchEvent(false);
            }
            break;
        }
        return super.onTouchEvent(ev);
    }

    class YScrollDetector extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return Math.abs(distanceY) > Math.abs(distanceX);
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        ViewGroup viewGroup = (ViewGroup) getParent();
        while (true) {
            if (viewGroup instanceof PullZoomVPScroll) {
                pullZoomVPScroll = (PullZoomVPScroll) viewGroup;
                break;
            }
            viewGroup = (ViewGroup) viewGroup.getParent();
        }
    }
}
